// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   AbstractResponseProcessor.java

package com.boci.common.processor;

import com.boci.clientapp.main.ApplicationService;
import com.boci.clientapp.main.ClientAppLoader;
import com.boci.common.constant.RequestTypeConst;
import com.boci.common.util.AppException;
import com.boci.common.util.MessagesConfigurator;
import com.boci.common.vo.ResponseVO;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

public abstract class AbstractResponseProcessor
{

    protected String getRespType()
    {
        return strRespType;
    }

    protected String getReqType()
    {
        return strReqType;
    }

    protected String getReturnCode()
    {
        return strRtnCode;
    }

    public AbstractResponseProcessor(String pXML, String pReqType, String pErrMsgPrefix)
        throws Exception
    {
        resultDocument = null;
        msgConf = ClientAppLoader.getMessagesConfigurator();
        appServ = ApplicationService.getInstance();
        strReqType = pReqType;
        if("".equals(pXML))
        {
            String errMsg = msgConf.getErrorMessage("SESSION_EXPIRED_OR_NOT_YET_LOGIN", appServ.getCurrentLocale());
            appServ.logout();
            throw new AppException("SESSION_EXPIRED_OR_NOT_YET_LOGIN", errMsg);
        }
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = null;
        docBuilder = docBuilderFactory.newDocumentBuilder();
        resultDocument = docBuilder.parse(new InputSource(new StringReader(pXML.trim())));
        resultDocument.getDocumentElement().normalize();
        if(isSessionExpired())
        {
            String errMsg = msgConf.getErrorMessage("SESSION_EXPIRED_OR_NOT_YET_LOGIN", appServ.getCurrentLocale());
            appServ.logout();
            throw new AppException("SESSION_EXPIRED_OR_NOT_YET_LOGIN", errMsg);
        } else
        {
            strRespType = getResponseType();
            chkRtnCode(pReqType, pErrMsgPrefix);
            return;
        }
    }

    protected boolean isSessionExpired()
    {
        return resultDocument.getFirstChild().getNodeName().equals("SESSION_EXPIRED_OR_NOT_YET_LOGIN");
    }

    public abstract ResponseVO processXML()
        throws Exception;

    protected void chkRtnCode(String pReqType, String pErrMsgPrefix)
        throws AppException
    {
        strRtnCode = getNodeValue("CODE");
        if(strRtnCode == null || strRtnCode.length() == 0)
            strRtnCode = "99";
        if(pReqType.equals(RequestTypeConst.BOCI_LOGIN_URL) && ClientAppLoader.isStandalone())
        {
            String strUpgradeCode = getNodeValue("UPGRADE_CODE");
            if(strUpgradeCode != null && strUpgradeCode.length() > 0 && !strUpgradeCode.equals("0"))
            {
                String strUpgradeLink = getNodeValue("UPGRADE_LINK");
                appServ.showUpgradeMsgDialog(true, strUpgradeLink, strUpgradeCode);
                String strErrMsg = "";
                if(!strRtnCode.equals("0"))
                {
                    strErrMsg = msgConf.getErrorMessage((new StringBuilder()).append(pErrMsgPrefix).append(strRtnCode).toString(), appServ.getCurrentLocale());
                    throw new AppException(strRtnCode, strErrMsg);
                }
            }
        }
        if(strRtnCode.equals("0"))
            return;
        String strErrCode = getNodeValue("ERROR_CODE");
        if(strErrCode == null || strErrCode.length() == 0)
        {
            String strErrMsg = msgConf.getErrorMessage((new StringBuilder()).append(pErrMsgPrefix).append(strRtnCode).toString(), appServ.getCurrentLocale());
            throw new AppException(strRtnCode, strErrMsg);
        } else
        {
            String strErrMsg = msgConf.getErrorMessage(strErrCode, appServ.getCurrentLocale());
            String strStrikePrice = getNodeValue("STRIKE_PRICE");
            String strLowerPrice = getNodeValue("LOWER_PRICE");
            strErrMsg = strErrMsg.replaceAll("%STRIKE_PRICE%", strStrikePrice);
            strErrMsg = strErrMsg.replaceAll("%LOWER_PRICE%", strLowerPrice);
            throw new AppException(strRtnCode, strErrCode, strErrMsg);
        }
    }

    protected String getResponseType()
    {
        return resultDocument.getFirstChild().getNodeName();
    }

    protected NodeList getNodeList(String pNodeName)
    {
        return resultDocument.getElementsByTagName(pNodeName);
    }

    protected String getNodeValue(String pNodeName)
    {
        NodeList node = resultDocument.getElementsByTagName(pNodeName);
        return getNodeValue(node);
    }

    protected String getNodeValue(Element element, String pNodeName)
    {
        NodeList node = element.getElementsByTagName(pNodeName);
        return getNodeValue(node);
    }

    protected String getNodeValue(NodeList pNodeList)
    {
        return getNodeValue(pNodeList.item(0));
    }

    protected String getNodeValue(Node pNode)
    {
        Element nodeElement = (Element)pNode;
        if(nodeElement == null)
            return "";
        NodeList textNodeList = nodeElement.getChildNodes();
        if(textNodeList.getLength() == 0)
            return "";
        else
            return textNodeList.item(0).getNodeValue().trim();
    }

    protected String getAttrValue(Node pNode, String pAttrName)
    {
        NamedNodeMap attrMap = pNode.getAttributes();
        for(int i = 0; attrMap != null && i < attrMap.getLength(); i++)
        {
            Node attrNode = attrMap.item(i);
            if(attrNode.getNodeName().equals(pAttrName))
                return attrNode.getNodeValue();
        }

        return "";
    }

    protected Document resultDocument;
    private String strRespType;
    private String strReqType;
    private String strRtnCode;
    protected MessagesConfigurator msgConf;
    protected ApplicationService appServ;
}
